package me.send.business.service.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.send.business.base.ErrorEntity;
import me.send.business.base.SendVariable;
import me.send.business.service.BaseService;
import me.send.business.service.mapper.OpencityMapper;
import me.send.business.service.pojo.Opencity;
import me.send.framework.util.BaseMap;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OpencityService extends BaseService {

	@Autowired
	private OpencityMapper opencityMapper;

	// 使用静态的成员变量 来加
	private static final String OPENCITY_LETTER = "A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z";
	private static final int OPEN_CITY_STATUS_OPEN=1;

	/**
	 * 查询 用户右边的 开放城市的列表 展示
	 * 
	 * 2015年6月9日09:26:50
	 * 
	 * 杨中仁
	 * 
	 */
	public Map<String, Object> list_all(String devicenumber) {
		// 实例化一个map 返回对象
		Map<String, Object> result = new HashMap<String, Object>();

		// 判断这个device
		// 返回下面的错误信息
		ErrorEntity entity = error_1000();
		// 取出当前开放的城市 2015年6月9日09:41:06
		List<Map<String, Object>> open_list = new ArrayList<Map<String, Object>>();
		// 取出下面待开放城市 2015年6月9日09:43:28 wait 待
		List<Map<String, Object>> wait_list = new ArrayList<Map<String, Object>>();
		if (isDevicenumber(devicenumber)) {
			// 开放状态是 0 的表示还没有开放的城市
			open_list = opencityMapper.list_openstatus(OPEN_CITY_STATUS_OPEN);
			// 开放的状态是 1的 表示已经开放的城市

			Opencity opencity = new Opencity();
			opencity.setOpenstatus(0);
			// 变量
			// List<Map<String, Object>> list = new
			// ArrayList<Map<String,Object>>();

			String[] opencityLetters = OPENCITY_LETTER.split("\\|");
			for (String letter : opencityLetters) {
				opencity.setLetter(letter);
				Map<String, Object> resultMap = new HashMap<String, Object>();
				resultMap.put("opencity",
						opencityMapper.list_opencity(opencity));
				wait_list.add(resultMap);
			}

			// open_list = opencityMapper.list_openstatus(1);
			// 错误信息等 成功
			// entity = error_1000();
		} else {
			entity = error_1002();
		}
		result.put(SendVariable.COMM_ERROR, entity); // 将错误的信息放在返回结果里面
		// 最后返回这个对象
		result.put("wait_list", wait_list);
		result.put("open_list", open_list);
		return result;
	}

	/**
	 * 安卓段获取城市列表
	 * @param devicenumber
	 * @return
	 */
	public Map<String, Object> citiesForAndroid(String devicenumber) {
		// 实例化一个map 返回对象
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> open_list = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> wait_list = new ArrayList<Map<String, Object>>();
		open_list = opencityMapper.list_openstatus(OPEN_CITY_STATUS_OPEN);
		wait_list = opencityMapper.citiesAll();
		result.put("openList", open_list);
		result.put("waitList", wait_list);
		return result;
	}

	public Map<String, Object> location_cityname(String devicenumber, String x,
			String y) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 判断是否有keyt
		ErrorEntity entity = error_1000();
		Map<String, Object> map3 = new HashMap<String, Object>();
		if (isDevicenumber(devicenumber)) {
			// 获取百度地图的 城市名称
			String cityname = BaseMap.GetLocationCityGeocoding(x, y);
			if (cityname != null && !cityname.equals("")) {
				// 判断cityname是否是null
				Map<String, Object> map2 = opencityMapper
						.opencity_cityname(cityname);
				if (map2 != null) {
					map3 = map2;
				}
			}
		} else {
			entity = error_1002();
		}
		// 将预定好的变量 放在map中 准备返回
		map.put(SendVariable.COMM_ERROR, entity);
		map.put(SendVariable.COMMON_OBJECT, map3);
		return map;
	}

	private String read() throws Exception {
		File file = new File("D:\\husong\\excel.txt");
		InputStream in = null;
		ByteArrayOutputStream byteout = new ByteArrayOutputStream();
		// 一次读一个字节
		in = new FileInputStream(file);
		int tempbyte;
		while ((tempbyte = in.read()) != -1) {
			byteout.write(tempbyte);
		}
		byte[] context = byteout.toByteArray();
		String string = new String(context, "gb2312");// 解码中文
		in.close();
		return string;
	}

	// 使用测试接口 2015年6月9日16:47:03
	public int opencity_save() {
		// 读取本地文件 处理
		try {
			String context = this.read();
			String[] textStrings = context.split("、");
			for (String text : textStrings) {
				Opencity opencity = new Opencity();
				opencity.setCreatetime(HelperUtil.getCurrentDate());
				opencity.setUpdatetime(HelperUtil.getCurrentDate());
				opencity.setName(text);
				opencity.setOpenstatus(0);
				opencityMapper.opencity_save(opencity);
			}
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	
	public int getCityIsOpenByName(String cityname){
		return opencityMapper.getCityIsOpenByName(cityname);
	}
}
